package StudentManager;

import java.util.ArrayList;
import java.util.Scanner;

import static java.lang.System.exit;

public class StudentManager {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> stu_Info = new ArrayList<>();   //全局变量，用于存储学生信息
        int choice;
        while(true){
            menu();
            System.out.println("Please input your choice (1-5) : ");
            choice = sc.nextInt();
            switch (choice){
                case 1:addStuInfo(stu_Info);break;
                case 2:deleteStuInfo(stu_Info);break;
                case 3:modifyStuInfo(stu_Info);break;
                case 4:showStuInfo(stu_Info);break;
                case 5:saveToFile(stu_Info);break;
                case 6:ExitSystem();
                default:
                    System.out.println("Your choice is error , please input again !!! \n");
            }
        }

    }

    public static void menu(){    //主菜单，显示功能选项
        System.out.println();
        System.out.println(" -----Welcome to use our Student Manager System !----- ");
        System.out.println("    1  add student info  ");
        System.out.println("    2  delete student info (delete by ID)  ");
        System.out.println("    3  modify student info (modify by  ID) ");
        System.out.println("    4  show all student info               ");
        System.out.println("    5  save all info to file (coming soon) ");
        System.out.println("    6  exit system                         ");
        System.out.println(" ------------------------------------------------------");
        System.out.println();
    }

    public static void addStuInfo(ArrayList<Student> als){   //添加学生信息
            Scanner sc = new Scanner(System.in);
        System.out.println("Please input how many students you want to add info :");
            int x = sc.nextInt();
            for(int i = 0; i < x; i++)
                addInfo(als,i);
        System.out.println();
        System.out.println("All the info has already been added !");
        System.out.println();
    }

    public static void deleteStuInfo(ArrayList<Student> als){                      //删除学生信息
        System.out.println("Please input the ID of the student that you want to delete info : ");
        Scanner sc = new Scanner(System.in);
        String ID = sc.nextLine();
        int idx = getID(ID,als);
        if( idx == -1){
            System.out.println("your operator is wrong,This ID don`t exist !!!");
        }
        else{
            als.remove(idx);
            System.out.println("The info of this student has already been deleted !");
        }
    }
    public static void modifyStuInfo(ArrayList<Student> als){                      //修改学生信息
        System.out.println("Please input the ID of the student that you want to modify info : ");
        Scanner sc = new Scanner(System.in);
        String ID = sc.nextLine();
        int idx = getID(ID,als);
        if( idx == -1){
            System.out.println("your operator is wrong,This ID don`t exist !!!");
        }
        else{
            System.out.println("Please input the new name of this student :");
            String name = sc.nextLine();
            als.get(idx).setName(name);
            System.out.println("Please input the new age of this student :");
            String age = sc.nextLine();
            als.get(idx).setAge(age);
            System.out.println("Please input the new address of this student :");
            String address = sc.nextLine();
            als.get(idx).setAddress(address);

            System.out.println("The info of this student has already been modified !");
        }
    }
    public static void showStuInfo(ArrayList<Student> als){                        //显示全部学生信息
        System.out.println(" ----------  student info  ---------- ");
        System.out.println();
        System.out.println("     id       name       age      address    ");
        for(int i = 0; i < als.size();i++){
            System.out.println("   "+als.get(i).getID()+"    " + als.get(i).getName()
                    + "        " + als.get(i).getAge() +"         " + als.get(i).getAddress());
        }
        System.out.println("That`s all the info of students !");
        System.out.println();
    }

   public static int getID(String id,ArrayList<Student> als){     //返回输入ID在学生信息表中的下标，如果不存在，则为 -1
        int pos = -1;
        for(int i = 0; i < als.size();i++)
            if( id.equals(als.get(i).getID()) )
                pos = i;
       return pos;
   }

   public static void saveToFile(ArrayList<Student> als){       //永久存储信息，待填坑

   }

    public static void ExitSystem(){           //退出系统
        System.out.println("You will exit system soon , welcome your use again !");
        exit(0);
    }
    public static void addInfo(ArrayList<Student> als,int i){     //实现一次添加信息操作
        Scanner sc = new Scanner(System.in);
        System.out.println("please input the ID of student " + (i+1) +" (6 bits): ");
        String ID = sc.nextLine();
        if(getID(ID,als) != -1){
            System.out.println("This info of this student is already exist !!!");
        }
        else{
            System.out.println("please input the name of student " + (i+1) +": ");
            String name = sc.nextLine();
            System.out.println("please input the age of student " + (i+1) +": ");
            String age = sc.nextLine();
            System.out.println("please input the address of student " + (i+1) +": ");
            String address = sc.nextLine();
            Student stu = new Student(ID,name,age,address);
            als.add(stu);
        }

    }

}


